package com.dong.router.api

import android.content.Context
import com.dong.router.api.model.Postcard
import com.dong.router.api.utils.LogUtils

/**
 * @author dong
 * @description:路由管理器
 * @date 2022/2/25 11:25
 */
class RouterManager private constructor() {

    private val routeMap: MutableMap<String?, Class<*>?> = mutableMapOf()

    private var loaded = false

    @JvmOverloads
    fun init(context: Context, isDebug: Boolean = false) {
        if (loaded) return

        RouterRegister.register(context)
        LogUtils.isDebug = isDebug
        loaded = true
    }

    fun register(path: String?, clazz: Class<*>?) {
        routeMap[path] = clazz
    }

    fun build(path: String?): Postcard {
        if(path == null){
            LogUtils.e("activity router path can not be null")
            return Postcard("")
        }
        if(routeMap[path]?.name.isNullOrBlank()){
            LogUtils.e("can not find target activity")
            return Postcard("")
        }
        return Postcard(routeMap[path]!!.name)
    }

    companion object {
        @JvmStatic
        val instance = RouterManager()
    }
}